//
//  SEPrescriptionDiagnosisNewCell.swift
//  SmartEyeForDoctor
//
//  Created by TT on 2021/5/10.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

enum SEPrescriptionDiagnosisType {
    case prefix, diagnose, suffix
}

class SEPrescriptionDiagnosisNewCell: UITableViewCell, UITextFieldDelegate {
    
    weak var delegate: SEPrescriptionNewCellDelegate?
    
    func update(with viewModel: SEPrescriptionViewModel, editEnable: Bool) {
        deleteBtn.isHidden = editEnable ? viewModel.isMainDiagnosis : true
        prefixLabel.text = viewModel.diagnosis.prefix
        diagnoseLabel.text = viewModel.diagnosis.middle
        suffixLabel.text = viewModel.diagnosis.suffix
        
        prefixView.isUserInteractionEnabled = editEnable
        diagnoseView.isUserInteractionEnabled = editEnable
        suffixView.isUserInteractionEnabled = editEnable
    }
    
    @IBOutlet private weak var prefixView: UIView! {
        didSet {
            let tap = UITapGestureRecognizer(target: self, action: #selector(tappedAction(_:)))
            prefixView.addGestureRecognizer(tap)
        }
    }
    
    @IBOutlet private weak var diagnoseView: UIView! {
        didSet {
            let tap = UITapGestureRecognizer(target: self, action: #selector(tappedAction(_:)))
            diagnoseView.addGestureRecognizer(tap)
        }
    }
    
    @IBOutlet private weak var suffixView: UIView! {
        didSet {
            let tap = UITapGestureRecognizer(target: self, action: #selector(tappedAction(_:)))
            suffixView.addGestureRecognizer(tap)
        }
    }
    
    @IBOutlet private weak var prefixLabel: UILabel!
    
    @IBOutlet private weak var diagnoseLabel: UILabel!
    
    @IBOutlet private weak var suffixLabel: UILabel!
    
    @IBOutlet private weak var deleteBtn: UIButton!
    
    @objc private func tappedAction(_ tap: UITapGestureRecognizer) {
        var type: SEPrescriptionDiagnosisType
        if prefixView == tap.view {
            type = .prefix
        } else if diagnoseView == tap.view {
            type = .diagnose
        } else {
            type = .suffix
        }
        delegate?.diagnosisNewCell(self, didTappedWithType: type)
    }
    
    @IBAction private func deleteAction(_ sender: UIButton) {
        delegate?.diagnosisNewCell(didTappedDelete: self)
    }
    
}


